3. Installing SQL Server 2012 Through the Command Line
In addition to installing SQL Server
using the Setup Wizard , SQL
Server can also be installed via the command line. Command-line
installations of SQL Server enable you to install, upgrade, or remove
SQL Server instances with little or no user interaction by specifying
silent, basic, or full user interface.
Every option available in the graphical user
interface using the Setup Wizard can also be configured via command-line
parameters. Sixty-eight parameters are available when running Setup.exe
from the command line to configure SQL Server features and services.
You can find the complete list of command line parameters at http://msdn.microsoft.com/en-us/library/ms144259(v=SQL.110).aspx.
Table 7 lists commonly used parameters for SQL Server Engine Services command-line installations.
Table 7 Commonly Used SQL Server 2012 Setup Command Line Parameters
/Q |
Optional. Specifies to run Setup in quiet mode for unattended installations. |
/QS |
Optional. Specifies to run Setup and show progress through the UI. No input is accepted or errors displayed. |
/UIMODE |
Optional. Specifies whether to display only essential dialog boxes during Setup. |
/ACTION |
Required. Specifies a workflow.
Supported values: Install, Uninstall, or Upgrade. |
/IACCEPTSQLSERVERLICENSETERMS |
Required. Accepts licensing terms. |
/UPDATEENABLED |
Optional. Specifies whether to look for product updates.
Supported values: true and false or 1 and 0. |
/UPDATESOURCE |
Optional. Specifies location of product updates.
Supported values: MU to search the Microsoft Update server, a folder path, a relative path, or a UNC share. |
/CONFIGURATIONFILE |
Optional. Specifies a configuration file to be used. |
/ERRORREPORTING |
Optional. Specifies error reporting opt-in option to Microsoft or a corporate report server.
Supported values: 1 = enabled; 0 = disabled. |
/FEATURES or /ROLE |
Required. Specifies components to install or specifies a predetermined configuration known as the setup role. |
/INSTALLSHAREDDIR |
Optional. Specifies a nondefault installation directory for 64-bit shared components. |
/INSTANCEDIR |
Optional. Specifies a nondefault installation directory for instance-specific components. |
/INSTANCEID |
Optional. Specifies a nondefault instance id. |
/INSTANCENAME |
Required. Specifies an instance name. |
/PID |
Optional. Specifies product key for the edition to be installed. If not specified, the Evaluation edition is installed. |
/INSTALLSQLDATADIR |
Optional. Specifies the directory for SQL Server data (.mdf) files. |
/SECURITYMODE |
Optional. Specifies security mode for SQL Server.
Default value: Windows-only authentication.Supported value: SQL. |
/SAPWD |
Required if /SECURITYMODE=SQL. Specifies sa SQL Server account password. |
/SQLUSERDBLOGDIR |
Optional. Specifies directory for SQL Server log files (*.ldf). |
/SQLTEMPDBDIR |
Optional. Specifies directory for SQL Server tempdb data file. |
/SQLTEMPDBLOGDIR |
Optional. Specifies directory for SQL Server tempdb log file. |
/SQLBACKUPDIR |
Optional. Specifies directory for SQL Server backup files (*.bak, *.trn). |
/SQLCOLLATION |
Optional. Specifies collation settings. |
/SQLSVCACCOUNT |
Required. Specifies the startup account for the SQL Server service. |
/SQLSVCPASSWORD |
Required. Specifies the password for the SQL Server service account. |
/SQLSVCSTARTUPTYPE |
Optional. Specifies the startup mode of the SQL Server service.
Supported values: Automatic, Manual, and Disabled. |
/SQLSYSADMINACCOUNTS |
Required. Specifies accounts to be added to the sysadmin role. |
Only a handful of these command-line parameters
are required to complete a SQL Server 2012 installation. Errors may
generate for required configuration options if no value is specified for
the corresponding parameter. For some parameters, default values are
automatically set when a value is not explicitly defined. You can use
the following script to install a standalone SQL Server 2012 instance
with all Database Services components:
Setup.exe /q /ACTION=Install /FEATURES=SQL /INSTANCENAME=MSSQLSERVER
/SQLSVCACCOUNT="<<DomainName\UserName>"
/SQLSVCPASSWORD="<MyPassword>"
/SQLSYSADMINACCOUNTS="<DomainName\UserName>" /AGTSVCACCOUNT="NT
AUTHORITY\Network
Service" /IACCEPTSQLSERVERLICENSETERMS
Command-line installations can also use
configuration files to reduce the number of command-line parameters and
standardize SQL Server deployments. Configuration files are created
during the installation process using the SQL Server installation
wizard. The path where the configuration file is specified in the bottom
section of the Ready to Install screen as shown in Figure 13, under the “Installing SQL Server 2012 Through the Installation Center” section.
Use the following script to execute an unattended installation of SQL Server:
Setup.exe /ConfigurationFile="<MyConfigurationFile.INI>"
4. Installing SQL Server 2012 Through PowerShell
You can also use PowerShell to perform
unattended installs. Simple PowerShell scripts can be written to execute
SQL Server 2012 Setup through its command-line interface. For example,
you can execute the command-line script used in the previous section
from the command line as follows:
$cmd = "d:\setup.exe /ACTION=install /Q /INSTANCENAME="MSSQLSERVER"
/IACCEPTSQLSERVERLICENSETERMS
/FEATURES=SQLENGINE,SSMS
/SQLSYSADMINACCOUNTS="YourDomain\Administrators";
Invoke-Expression -command $cmd | out-null;
More complex PowerShell scripts can be written for
larger SQL Server 2012 deployments. A common approach is the use of
PowerShell functions that accept the setup parameters necessary to
perform unattended installations. These PowerShell functions are then
executed in batches or inside a process that loops through a list of
server names with corresponding parameters.
For example, a PowerShell function can be saved in
a PowerShell script file and called along with setup parameters to
perform a large scale unattended deployment of SQL Server 2012. Listing 1 provides an example of a PowerShell function that you can use for SQL Server 2012 unattended installations.
Listing 1 Install-Sql2012.ps1
The PowerShell function contained in the Install-SQL2012.ps1 script file is as follows:
Function Install-Sql2012
{
param
(
[Parameter(Position=0,Mandatory=$false)][string] $Path,
[Parameter(Position=1,Mandatory=$false)][string] $InstanceName =
"MSSQLSERVER",
[Parameter(Position=2,Mandatory=$false)][string] $ServiceAccount,
[Parameter(Position=3,Mandatory=$false)][string] $ServicePassword,
[Parameter(Position=4,Mandatory=$false)][string] $SaPassword,
[Parameter(Position=5,Mandatory=$false)][string] $LicenseKey,
[Parameter(Position=6,Mandatory=$false)][string] $SqlCollation =
"SQL_Latin1_General_CP1_CI_AS",
[Parameter(Position=7,Mandatory=$false)][switch] $NoTcp,
[Parameter(Position=8,Mandatory=$false)][switch] $NoNamedPipes
)
#Build the setup command using the install mode
if ($Path -eq $null -or $Path -eq "")
{
#No path means that the setup is in the same folder
$command = ‘setup.exe /Action="Install"'
}
else
{
#Ensure that the path ends with a backslash
if(!$Path.EndsWith("\"))
{$Path += "\"
}
$command = $path + ‘setup.exe /Action="Install"'
}
#Accept the license agreement - required for command line installs
$command += ‘ /IACCEPTSQLSERVERLICENSETERMS'
#Use the QuietSimple mode (progress bar, but not interactive)
$command += ‘ /QS'
#Set the features to be installed
$command += ‘ /FEATURES=SQLENGINE,CONN,BC,SSMS,ADV_SSMS'
#Set the Instance Name
$command += (’ /INSTANCENAME="{0}"’ -f $InstanceName)
#Set License Key only if a value was provided,
#else install Evaluation edition
if ($LicenseKey -ne $null -and $LicenseKey -ne "")
{
$command += (’ /PID="{0}"’ -f $LicenseKey)
}
#Check to see if a service account was specified
if ($ServiceAccount -ne $null -and $ServiceAccount -ne "")
{
#Set the database engine service account
$command += (’ /SQLSVCACCOUNT="{0}" /SQLSVCPASSWORD="{1}"
/SQLSVCSTARTUPTYPE="Automatic"’ -f
$ServiceAccount, $ServicePassword)
#Set the SQL Agent service account
$command += (’ /AGTSVCACCOUNT="{0}" /AGTSVCPASSWORD="{1}"
/AGTSVCSTARTUPTYPE="Automatic"’ -f
$ServiceAccount, $ServicePassword)
}
else
{
#Set the database engine service account to Local System
$command += ‘ /SQLSVCACCOUNT="NT AUTHORITY\SYSTEM"
/SQLSVCSTARTUPTYPE="Automatic"'
#Set the SQL Agent service account to Local System
$command += ‘ /AGTSVCACCOUNT="NT AUTHORITY\SYSTEM"
/AGTSVCSTARTUPTYPE="Automatic"'
}
#Set the server in SQL authentication mode if SA password was provided
if ($SaPassword -ne $null -and $SaPassword -ne "")
{
$command += (’ /SECURITYMODE="SQL" /SAPWD="{0}"’ -f $SaPassword)
}
#Add current user as SysAdmin
$command += (’ /SQLSYSADMINACCOUNTS="{0}"’ -f
[Security.Principal.WindowsIdentity]::GetCurrent().Name)
#Set the database collation
$command += (’ /SQLCOLLATION="{0}"’ -f $SqlCollation)
#Enable/Disable the TCP Protocol
if ($NoTcp)
{
$command += ‘ /TCPENABLED="0"'
}
else
{
$command += ‘ /TCPENABLED="1"'
}
#Enable/Disable the Named Pipes Protocol
if ($NoNamedPipes)
{
$command += ‘ /NPENABLED="0"'
}
else
{
$command += ‘ /NPENABLED="1"'
}
if ($PSBoundParameters['Debug'])
{
Write-Output $command
}
else
{
Invoke-Expression $command
}
}
After you download Listing 1 from the companion website, save it to a folder, for example c:\scripts.
Because this is a file that you download from the Internet, you may be
required to right-click the file and unblock it. When downloaded,
execute this function by following these steps.
1. Launch the
PowerShell command line with elevated Administrator privileges by
right-clicking the PowerShell executable and selecting Run as
Administrator. The PowerShell command line opens.
2. Verify that you can run and load unsigned PowerShell scripts and files. In the PowerShell command line, type get-executionpolicy to verify the current execution policy. If it is not set to RemoteSigned, you need to change it to this value by executing the following command:
Set-ExecutionPolicy RemoteSigned
Next, load the PowerShell function in the script file by executing the following command:
c:\scripts\Install-Sql2012.ps1
3. Notice the . and blank space before the script file path. The . and blank space is a required character to dot-source the script file.
4. Verify that the function has been loaded by issuing the following command:
get-command Install-Sql2012
A single row is returned showing CommandType Function and Name Install-Sql2012.
5. At this point you are ready to invoke the PowerShell function you just loaded. Invoke the Install-Sql2012 as follows:
Install-Sql2012 -Param1 Param1Value -Param2 Param2Value
For example, the following command invokes the Install-Sql2012
function and sets the SQL Server service account and password along
with the Instance Name and initiates a SQL Server 2012 installation.
Install-Sql2012 -Path d:\ -ServiceAccount "winserver\Administrator"
-ServicePassword "P@ssword"
-SaPassword "P@ssword"
-InstanceName "MyInstanceName"
Note
The SQL Server 2012 installation path may differ depending on your installation media.
Figure 15 shows the PowerShell command-line window with the steps necessary to invoke the Install-Sql2012 function.